package com.aiju.cash.ui.multiple_shops.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiju.bean.Shop;
import com.aiju.bean.User;
import com.aiju.cash.R;
import com.aiju.cash.ui.multiple_shops.en.ShopManagerListType;
import com.aiju.common.image.AiJuImageLoaderProxy;
import com.aiju.utils.StringUtil;
import com.aiju.utils.Util;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/12.
 * 门店管理 adapter
 *
 * @author 流冥
 * @date 2017-06-12
 */

public class ShopManagerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>   {
    private List<Shop> list = new ArrayList<>();
    private Context mContext;
    private ShopManagerListType mType;
    private User mUser;

    public ShopManagerAdapter(Context context, List<Shop> shopList, ShopManagerListType type, User user) {
        mContext = context;
        mType = type;
        this.list = shopList;
        mUser = user;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item_shop_manager, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);
        return new ShopManagerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        ShopManagerViewHolder holder = (ShopManagerViewHolder) viewHolder;
//        holder.position = i;
        Shop manager = list.get(i);
        showView(((ShopManagerViewHolder) viewHolder), manager);

    }



    @Override
    public int getItemCount() {
        return list.size();
    }


    class ShopManagerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public View rootView;
        public ImageView image;
        public TextView title;
        public TextView subTitle;
        public TextView audit_state;
        public ImageView check;
        public ImageView shopType;
        public int position;

        public ShopManagerViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.image);
            check = (ImageView) itemView.findViewById(R.id.check);
            shopType = (ImageView) itemView.findViewById(R.id.shop_type);
            title = (TextView) itemView.findViewById(R.id.title);
            subTitle = (TextView) itemView.findViewById(R.id.sub_title);
            audit_state = (TextView) itemView.findViewById(R.id.audit_state);
            rootView = itemView.findViewById(R.id.parent_view);
            rootView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (null != onRecyclerViewListener) {
                switch (mType) {
                    case SHOPMANAGER://店铺管理----跳转至   查看门店信息界面
                        onRecyclerViewListener.onManageShop(list.get(position));
                    case CHANGESHOP://店铺切换 ----返回app首页，并刷新数据
                        onRecyclerViewListener.onChangeShop(list.get(position));
                        break;
                }
            }
        }
    }

    private OnRecyclerViewListener onRecyclerViewListener;

    public interface OnRecyclerViewListener {
        void onChangeShop(Shop shopManager);

        void onManageShop(Shop shopManager);
    }

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    /**
     * 根据type进行适配
     * SHOPMANAGER：店铺管理
     * CHANGESHOP：店铺切换
     */
    private void showView(ShopManagerViewHolder viewHolder, Shop shopManager) {
        showCommonView(viewHolder, shopManager);
        switch (mType) {
            case SHOPMANAGER:

                break;
            case CHANGESHOP:
                // TODO: 2017/6/12 判断进入的店铺和列表一致，显示打钩图片
//                if () {
//                    viewHolder.check.setVisibility(View.VISIBLE);
//                } else {
//                    viewHolder.check.setVisibility(View.GONE);
//                }
                if(mUser.getStoreId().equals(shopManager.getStoreId())){
                    viewHolder.check.setVisibility(View.VISIBLE);
                }else{
                    viewHolder.check.setVisibility(View.GONE);
                }

                break;
        }
    }

    /**
     * 显示公有view
     */
    private void showCommonView(ShopManagerViewHolder viewHolder, Shop shopManager) {
        AiJuImageLoaderProxy.getInstance().displayImage(shopManager.getImageUrl(), viewHolder.image, R.mipmap.icon_empty);
        viewHolder.title.setText(shopManager.getShopName());
        setIsReputationShopTextView(viewHolder.subTitle, shopManager.getIsReputationShop());
        if (StringUtil.isBlank(shopManager.getReviewStatus())) {
            viewHolder.audit_state.setText("");
        } else {
            showCheckStateTextView(shopManager.getReviewStatus(), viewHolder.audit_state);
        }
        showLeftTopImageByTag(shopManager.getShopType(), viewHolder.shopType);
    }


    /**
     * 显示item左上角商家店铺类型：总店、直营点、加盟店
     * 门店类型（0,总店 1，直营店，2加盟店）
     */
    private void showLeftTopImageByTag(String shopTag, ImageView imageView) {
        switch (shopTag) {
            case "0":
                imageView.setImageResource(R.mipmap.icon_left_zongdian);
                break;
            case "1":
                imageView.setImageResource(R.mipmap.icon_left_zhiying);
                break;
            case "2":
                imageView.setImageResource(R.mipmap.icon_left_jiameng);
                break;
            default:
                break;
        }
    }

    /**
     * 审核状态:
     * 未通过 0
     * 已通过 1
     * 审核中 2
     */
    public void showCheckStateTextView(String shopTag, TextView textView) {
        switch (shopTag) {
            case "0":
                setTextAndColor(textView, "未通过", R.color.shop_manager_weitongguo);
                break;
            case "1":
                setTextAndColor(textView, "已通过", R.color.shop_manager_yitonguo);
                break;
            case "2":
                setTextAndColor(textView, "审核中", R.color.shop_manager_shenhezhong);
                break;
            case "3":
                setTextAndColor(textView, "未上传", R.color.shop_manager_shenhezhong);
                break;
            default:
                break;
        }
    }

    /**
     * 设置textview 文字和颜色
     */
    private void setTextAndColor(TextView textView, String text, int color_res) {
        textView.setText(text);
        textView.setTextColor(Util.getColor(color_res));
    }

    /**
     * 口碑店铺（1）--，普通店铺（0）
     */
    private void setIsReputationShopTextView(TextView textView, String isReputationShop) {
        if ("1".equals(isReputationShop)) {
            textView.setText("口碑店铺");
        } else if ("0".equals(isReputationShop)) {
            textView.setText("普通店铺");
        }

    }


}
